package com.liu.springmvc.view;

import java.io.File;
import java.util.Locale;

/**
 * @author LIU
 * @version 1.0
 * @date 2024/5/16 17:43
 * @description TODO
 */

public class MyViewResolver implements ViewResolver{
    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir;

    public MyViewResolver(String templateRoot) {

//        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        this.templateRootDir = new File(templateRoot);
    }

    @Override
    public View resolveViewName(String viewName) {
        if(null == viewName || "".equals(viewName)){
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : (viewName + DEFAULT_TEMPLATE_SUFFIX);
        //src/main/resources/layouts/404.html
        String template = templateRootDir.getPath() + "/" + viewName;
        template = template.replaceAll("/+", "/");

        File templateFile = new File(template);


        return new MyView(templateFile);
    }
}
